package com.sunny.shop.service;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.core.LocalUser;
import com.sunny.shop.exception.httpe.NotFoundException;
import com.sunny.shop.model.Category;
import com.sunny.shop.model.Collect;
import com.sunny.shop.model.Song;
import com.sunny.shop.repository.CategoryRepository;
import com.sunny.shop.repository.CollectRepository;
import com.sunny.shop.repository.SongRepository;
import com.sunny.shop.vo.GoodsSimplifyVO;
import com.sunny.shop.vo.SongVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SongServiceImpl {
    @Value("${sunny.oss.url}")
    private String baseUrl;
    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private SongRepository songRepository;

    @Autowired
    private CollectRepository collectRepository;

    public List<Category> getCateList(Long pid){
        List<Category> allByPid = categoryRepository.findAllByPid(pid);
        return allByPid;
    }

    public List<Song> getSongList(){
        PageRequest page = PageRequest.of(0, 30);
        List<Song> limitAll = songRepository.findLimitAll(page);
        return limitAll;
    }

    /**
     * 滚动入口歌曲
     * @param id
     * @return
     */
    public SongVO getSongById(Long id){
        Long uid = LocalUser.getUser().getId();
        Song res = songRepository.findOneById(id);
        if(res == null) {
            throw new NotFoundException(70001);
        }

        String orgPlayUrl = res.getPlayUrl();
        String newPlayUrl = baseUrl + "/" + orgPlayUrl;
        res.setPlayUrl(newPlayUrl);
        res.setImg(baseUrl + "/" + res.getImg());

        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        SongVO songVO = mapper.map(res, SongVO.class);
        Collect collect = collectRepository.findOneByTopicIdAndUid(id, uid);
        if(collect != null && collect.getStatus() == 1){
            songVO.setIsCollect(true);
        }else{
            songVO.setIsCollect(false);
        }
        return songVO;
    }

    /**
     * 根据分类卡片获取歌曲
     * @param cateId
     * @return
     */
    public ArrayList<SongVO> getSongByCateId(Long cateId){
        PageRequest page = PageRequest.of(0, 30);
        List<Song> limitAll = songRepository.findLimitByCateId(cateId, page);
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        ArrayList<SongVO> voList = new ArrayList<>();

        Long uid = LocalUser.getUser().getId();
        Integer collectType = 2;
        Integer status = 1;
        ArrayList<Long> collect = collectRepository.selectorderby(uid, collectType, status);

        limitAll.forEach(s->{
            String orgPlayUrl = s.getPlayUrl();
            String newPlayUrl = baseUrl + "/" + orgPlayUrl;
            s.setPlayUrl(newPlayUrl);
            SongVO songVO = new SongVO();
            mapper.map(s, songVO);
            Long topicId = songVO.getId();
            if(collect.contains(topicId)){
                songVO.setIsCollect(true);
            }else{
                songVO.setIsCollect(false);
            }
            voList.add(songVO);
        });
        return voList;
    }
}
